/*
 * Copyright © 2021 http://www.hn-szzy.com/ All rights reserved.
 */
package com.xjh.utils.exception;

import com.xjh.utils.resultutils.ResponseObj;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;
import javax.xml.bind.ValidationException;

/**
 * @author： 徐建华
 * @date： 2021/12/29
 * @description：全局异常处理器
 */
//使用 Rest则不用在方法上加ResponseBody才能返回给前端json格式
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 自定义验证异常
     */
    @ExceptionHandler(SysException.class)
    public ResponseObj handleSysException(SysException e)
    {
        log.info("SysException异常处理器被触发了");
        e.printStackTrace();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.error(e.getMessage()+e.getCode());
    }
    /**
     * 捕获参数校验异常：必须得定义异常处理器，否则前台只能看到400、bad request 无法看到校验信息
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseObj handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
    {
        log.info("MethodArgumentNotValidException异常处理器被触发了");
        e.printStackTrace();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.error(e.getBindingResult().getFieldError().getDefaultMessage()+"======参数校验异常了");
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public ResponseObj handleValidationException(ValidationException e) {
        log.info("ValidationException异常处理器被触发了");
        log.error(e.getMessage(), e);
        return new ResponseObj().error( e.getCause().getMessage());
    }

    /**
     * ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseObj handleConstraintViolationException(ConstraintViolationException e) {
        log.info("ConstraintViolationException异常处理器被触发了");
        log.error(e.getMessage(), e);
        return new ResponseObj().error( e.getCause().getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseObj handlerNoFoundException(Exception e) {
        log.info("NoHandlerFoundException异常处理器被触发了");
        log.error(e.getMessage(), e);
        return new ResponseObj().error( e.getCause().getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseObj handleDuplicateKeyException(DuplicateKeyException e) {
        log.info("DuplicateKeyException异常处理器被触发了");
        log.error(e.getMessage(), e);
        return new ResponseObj().error( e.getCause().getMessage());
    }


//    @ExceptionHandler(Exception.class)
//    public ResponseObj handleException(Exception e) {
//        log.info("Exception异常处理器被触发了");
//        log.error(e.getMessage(), e);
//        return new ResponseObj().error( e.getCause().getMessage());
//    }



}
